function InitBluetooth(callback){
	// uni.setStorageSync("deviceId","");
	// uni.setStorageSync("serviceId","");
	// uni.setStorageSync("characteristics","");
	uni.openBluetoothAdapter({
	  success: (res) => {
		console.log('第一步、蓝牙初始化成功', res)
		uni.startBluetoothDevicesDiscovery({
		  allowDuplicatesKey: false,
		  powerLevel: "high",  //设置加快搜索速度
		  success: (res)=>{
			  GetDevices(callback);
		  }
		})
	  },
	  fail: (res) => {
		uni.showToast({ title: '蓝牙初始化失败', icon: 'none' })
		let list=new Array();
		if(callback){callback(list);}
	  }
	});
};
function GetDevices(callback){
	var List=new Array();
	uni.getBluetoothDevices({
	 success:  (res) =>{
	 console.log("成功",res)//res:{errMsg: "getBluetoothDevices:ok", devices: Array(3)}
	 
	 res.devices.forEach(item=>{
		 if(item.name.indexOf("H96")>-1){
			 List.push(item);
		 }
	 })
	 }
	});
	uni.onBluetoothDeviceFound((res)=> {
		res.devices.forEach(item=>{
			 if(item.name.indexOf("H96")>-1){
				 uni.setStorageSync("deviceId",item.deviceId);
				 Connect();
				 List.push(item);
				 if(List.length==1 && callback){callback(List);}
			 }
		})
		setTimeout(function(){
			uni.stopBluetoothDevicesDiscovery({
			  success(res) {
			  }
			})
			if(List.length==0 && callback){callback(List);}
		},3000);
		
	})
};
function Connect(callback){
	var deviceId=uni.getStorageSync("deviceId");
	var conn=false;
	uni.createBLEConnection({
	 deviceId,//上面选择蓝牙设备的deviceId,例：连接第一个设备devices[0].deviceId
	 success:(res)=> {
		 conn=true;
		//获取蓝牙设备所有服务
		setTimeout(t=>{
			uni.getBLEDeviceServices({
			 deviceId,//已连接的蓝牙设备ID
			 success: (res)=> {
				 var serviceId = res.services[2].uuid;
				 uni.setStorageSync("serviceId",serviceId);
				 uni.getBLEDeviceCharacteristics({ 
					  deviceId,
					  serviceId,
					  success: (res)=> {
						  for (var i = 0; i < res.characteristics.length; i++) {
							  var item = res.characteristics[i];
							  if (item.properties.write) { 
								var characteristics = item.uuid.toString(); 
								uni.setStorageSync("characteristics",characteristics);
								if(callback){callback(true);}
							  }
						  }
					  },fail(res) {
						if(callback){callback(false);}
					  }
				 })
			 },
			 fail(res) {
				if(callback){callback(false);}
			 }
			});
		},2000);
		//停止搜寻附近的蓝牙外围设备
		uni.stopBluetoothDevicesDiscovery({
			success(res) {
				console.log("停止搜寻附近的蓝牙外围设备："+res)
			}
		});
	 },
	 fail:res=>{
	 	if(callback){callback(false);}
	 }
	})
	setTimeout(()=>{
		if(!conn){
			uni.stopBluetoothDevicesDiscovery({
				success(res) {
					console.log("停止搜寻附近的蓝牙外围设备："+res)
				}
			});
			if(callback){callback(false);}
		}
	},3000);
	
};
function BeginPrint(buffer,callback){
	var bytelength=buffer.byteLength;
	if(bytelength>0){
		uni.writeBLECharacteristicValue({
			deviceId: uni.getStorageSync("deviceId"),
			serviceId: uni.getStorageSync("serviceId"),
			characteristicId: uni.getStorageSync("characteristics"),
			value: buffer.slice(0, bytelength > 20 ? 20 : bytelength),
			success: (res)=> {
				BeginPrint(buffer.slice(20, bytelength), callback);	
				if(bytelength<=20 && callback){
					callback();
				}
			},
			fail(res) {
				console.log('打印失败'); 
				Util.Toast("请先连接打印机");
			}
		});
	}
};
function CloseBLEConnection(callback){
	var deviceId=uni.getStorageSync("deviceId");
	uni.closeBLEConnection({
	  deviceId,
	  success(res) {
	    if(callback){callback();}
	  }
	});
};
module.exports={
	InitBluetooth,
	GetDevices,
	BeginPrint,
	Connect,
	CloseBLEConnection
}